package com.jf3q.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class OssUtils {
    @Value("${ali.endpoint}")
    private String endpoint;
    @Value("${ali.endpoint2}")
    private String endpoint2;
    @Value("${ali.bucketName}")
    private String bucketName;
    @Value("${ali.accessKeyId}")
    private String accesskeyId;
    @Value("${ali.accessKeySecret}")
    private String secretAccesskey;

    public String upload(String type, MultipartFile file){
        if (file==null || file.isEmpty()) {
            throw new RuntimeException("文件必填");
        }

        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        SimpleDateFormat format= new SimpleDateFormat("yyyy/MM/dd");
        String dataPath = format.format(new Date());

        String fileName=dataPath+"/"+ UUID.randomUUID().toString().replace("-","")+suffix;

        StringBuffer objectName = new StringBuffer();
        objectName.append(type+"/");

        objectName.append(fileName);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint,accesskeyId,secretAccesskey );

        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName.toString(), file.getInputStream());

            // 上传文件。
            ossClient.putObject(putObjectRequest);
            String url="https://"+bucketName+"."+endpoint2+"/"+objectName;
            return url;
        } catch (Exception e) {

            throw new RuntimeException("上传阿里云失败");
        }   finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

    }



    public Boolean del(String path){
        if(StringUtils.hasText(path)){

            //https://jf3q.oss-cn-beijing.aliyuncs.com/banner/2024/06/03/9fafd162018048878bda1205d65f1b7f.jpg
            //banner/2024/06/03/9fafd162018048878bda1205d65f1b7f.jpg
            String objectName = path.substring(path.indexOf(endpoint2)+endpoint2.length()+1);

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint,accesskeyId,secretAccesskey );

            try {
                // 删除文件或目录。如果要删除目录，目录必须为空。
                ossClient.deleteObject(bucketName, objectName);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }else{
            return false;
        }


    }
}
